/**
  ******************************************************************************
  * @file   bsp_sht20.c
  * @brief  温湿度传感器SHT20驱动
  *   
  ******************************************************************************
  */
#include "bsp_sht20.h"
#include "i2c.h"
#include "stdio.h"
#include "usart.h"
#include "string.h"
#include "wifi.h"
//
volatile SHT20_TemRH_Val gTemRH_Val;	//存储温湿度值
//
/*
**********************************************************************
* @fun     :BSP_SHT20_Read 
* @brief   :根据指令读取sht20内部的寄存器
* @param   :
* @return  :sht20_reg_val 
**********************************************************************
*/
uint16_t BSP_SHT20_Read(uint8_t sht20_cmd)
{
		uint16_t sht20_reg_val = 0;
		uint8_t sht20_reg_buff[2]={0x00,0x00};
		HAL_I2C_Master_Transmit(&hi2c1,SHT20_ADDR_WRITE,&sht20_cmd,1,100);    //发送控制指令
		HAL_Delay(10);
		HAL_I2C_Master_Receive(&hi2c1,SHT20_ADDR_READ,sht20_reg_buff,2,100);      //读取数据，两个字节
		HAL_Delay(10);
		sht20_reg_val=(sht20_reg_buff[0]<<8)|sht20_reg_buff[1];										//合并数据
	
		return (sht20_reg_val);
}
/*
**********************************************************************
* @fun     :BSP_SHT20_GetData 
* @brief   :获取sht20的温湿度数据
* @param   :
* @return  :none
**********************************************************************
*/
void BSP_SHT20_GetData(void)
{	
	extern int fan_state;
	extern int motor_state;
	extern int hot_state;
	extern int beep_state;
	extern int cold_state;
	extern int mode ;
	int  key_ledhot = 304;
	int  key_ledclod =305;
	int  key_motor =306;
	int  key_beep =307;
	int  key_temp =301;
	int  key_fan  =303;
	int  key_humi =302;
	int  hot_key  =2;
	int  humi_key =3 ;
	int  temp_key =3;
	int  motor_key=2;
	int  fan_key  =2;
	int  beep_key =2;
	int  cold_key =2;
	int  dev_type =111;
	extern char tmp[512];
	extern char hum[256];
	uint16_t  pTem=0,pHum=0;
	//读取数据
	pTem=BSP_SHT20_Read(SHT20_HOLD_M_READ_T);
	pHum=BSP_SHT20_Read(SHT20_HOLD_M_READ_RH);
	pHum &=0xFFFC;
	//数据转换
	gTemRH_Val.Tem	= -46.85f+175.72f*((float)pTem/65536);
	gTemRH_Val.Hum  = -6+125*((float)pHum/65536);
	if(mode ==0){
		if(gTemRH_Val.Tem>20&&gTemRH_Val.Tem<30){
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,0);
	}else{
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);
	}
	}
//	if(gTemRH_Val.Tem<25){
//		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
//	}else if(gTemRH_Val.Tem>30){
//		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
//	}
	fan_state =HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_6);
	motor_state = HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_7);
	hot_state =HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4);
	cold_state =HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_3);
	beep_state =HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_15);
	sprintf(tmp,"{\"data\":[{\"key\":%d,\"name\":\"%s\",\"type\":%d,\"value\":\"%.2f\",\"dev_type\":%d},{\"key\":%d,\"name\":\"%s\",\"type\":%d,\"value\":\"%.2f\",\"dev_type\":%d},{\"key\":%d,\"name\":\"%s\",\"type\":%d,\"value\":%d,\"dev_type\":%d},{\"key\":%d,\"name\":\"%s\",\"type\":%d,\"value\":%d,\"dev_type\":%d},{\"key\":%d,\"name\":\"%s\",\"type\":%d,\"value\":%d,\"dev_type\":%d},{\"key\":%d,\"name\":\"%s\",\"type\":%d,\"value\":%d,\"dev_type\":%d},{\"key\":%d,\"name\":\"%s\",\"type\":%d,\"value\":%d,\"dev_type\":%d}]}",key_humi,"humi",humi_key,gTemRH_Val.Hum,dev_type,key_temp,"temp",temp_key,gTemRH_Val.Tem,dev_type,key_fan,"fan",fan_key,fan_state,dev_type,key_ledhot,"hot",hot_key,hot_state,dev_type,key_ledclod,"cold",cold_key,cold_state,dev_type,key_beep,"beep",beep_key,beep_state,dev_type,key_motor,"motor",motor_key,motor_state,dev_type);
	USART_printf(&huart1,tmp);	
	WIFI_SendData(tmp);
}

